package com.game.server.message;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.ByteBuffer;
import org.apache.log4j.Logger;

import com.comm.IRoute;
import com.comm.ISession;
import com.game.majiang.logic.LogicServer;
import com.game.server.system.AbstractClient;
public class Route implements IRoute {

	private Logger logger = Logger.getLogger(this.getClass().getName());


	@Override
	public void receive(ISession session, ByteBuffer message) {
		try {
			MessageHandler.getInstance().recMessage(session, (ByteBuffer) message);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("服务器异常断开连接!       "+e);
			StringWriter errors = new StringWriter();  
			e.printStackTrace(new PrintWriter(errors));  
			logger.error(" exception:"+e.getMessage()+"\t"+errors.toString()+"    ......."); 
			session.close();
		}
	}

	@Override
	public void close(ISession session) {
		AbstractClient  c  = LogicServer.getInstance().getClient(session.getId());
		if(c != null)
			try {
				//log.info("心跳:"+c.getUserId());
				c.Close();
			} catch (Exception e) {
				e.printStackTrace();
				StringWriter errors = new StringWriter();  
				e.printStackTrace(new PrintWriter(errors));  
				logger.error(" close exception:"+e.getMessage()+"\t"+errors.toString()+"    ......."); 
			}
			
	}
}
